﻿using System;
using System.Data;
using System.Linq;
namespace WPFExcelReport
{
    public partial class ITXEXRPT
    {

        private void JLTGB20LABEL()
        {
            DataRow dwMaster = null;
            DataRow dwABK = null;
            int rStart = 0;
            int nRow = 1;
            int idx = 0;
            dwMaster = dsSource.Tables["ABPOS"].Rows[0];
            dwABK = dsSource.Tables["ABKOPF"].Rows[0];
           DataRow  dwMU = dsSource.Tables["ABAUFM"].Rows[0];
           exAgent.SetCurrentSheet("JLTGB20LABELT");
           exAgent.SetCellValue(1, 4, dwMU["ABAKDAUFTNR"].ToString().Trim());
           exAgent.SetCurrentSheet("JLTGB20LABEL");
           exAgent.SetCellValue(1, 5, dwABK["ABKKDAUFTNR"].ToString().Trim() + "(" + GetGB2312Value(dwMU["ABABEM2"].ToString().Trim()) + ")");
            string KRS = dwMU["ABAAUFKRS"].ToString();
            string LFD = dwMU["ABAAUFLFD"].ToString();
            string POS = dwMU["ABAAUFPOS"].ToString();
            var qAB = from DataRow row in dsSource.Tables["ABPOS"].AsEnumerable()
                      where row["ABPAUFKRS"].ToString() == KRS
                      && row["ABPAUFLFD"].ToString() == LFD
                      && row["ABPAUFPOS"].ToString() == POS
                      select row;
            foreach (DataRow row in qAB)
            {
                dwMaster = row;
            }
            string art = dwMaster["ABPART"].ToString();
            string var = dwMaster["ABPVAR"].ToString();
            string color = dwMaster["ABPFARBE"].ToString();
            #region====Header======
            exAgent.SetCellValue(2, 5, art.Trim());
            var qGS = from DataRow row in dsSource.Tables["GSDAT"].AsEnumerable()
                      where row["GSART"].ToString() == art
                      && row["GSVAR"].ToString() == var
                      select row;
            foreach (DataRow row in qGS)
            {
                exAgent.SetCellValue(3, 5, GetGB2312Value(row["GSHANDLNR"].ToString().Trim()));
            }  
            //exAgent.SetCellValue(1, 11, "生产单：" + KRS + "-" + Helpers.Helper.GetSubRollNum(LFD) + "-" + Helpers.Helper.GetSubRollNum(POS));//barcode
            
            exAgent.SetCellValue(5, 5, color);
            var qGC = from DataRow row in dsSource.Tables["GSCOLOR"].AsEnumerable()
                      where row["GSCART"].ToString() == art
                      && row["GSCVAR"].ToString() == var
                      && row["GSCFARBE"].ToString() == color
                      select row;
            foreach (DataRow row in qGC)
            {
                exAgent.SetCellValue(4, 13, GetGB2312Value(row["GSCBEZ"].ToString().Trim()));
            } 
            exAgent.SetCellValue(6, 5, dwMU["ABAKDPARTIE"].ToString().Trim());
            exAgent.SetCellValue(4, 5, dwMU["ABAKDAUFTNR"].ToString().Trim());
            string prdate = System.DateTime.Now.Year.ToString() + "-" + System.DateTime.Now.Month.ToString() + "-"
                            + System.DateTime.Now.Day.ToString();
            exAgent.SetCellValue(8, 5, prdate);
            exAgent.SetCellValue(2, 13,  GetGB2312Value(dwMU["ABABEM1"].ToString().Trim()));
            #endregion

            #region====yarn=========
            string sppartie = "";
            rStart = 16;
            nRow = 2; 
            idx = 0;
            int ycount = dsSource.Tables["GAAUF"].Rows.Count;
            foreach (DataRow ga in dsSource.Tables["GAAUF"].Rows)
            {
                if (ga["GASA"].ToString() == "310")
                {
                    ycount--;
                    continue;
                } 
                #region=====Alloction=======
                if (dsSource.Tables.Contains("ZUTEI"))
                {
                    var qzu = from rows in dsSource.Tables["ZUTEI"].AsEnumerable()
                              where rows["ZUAUFKRS"].ToString() == ga["GAAUFKRS"].ToString()
                                 & rows["ZUAUFLFD"].ToString() == ga["GAAUFLFD"].ToString()
                                & rows["ZUAUFPOS"].ToString() == ga["GAAUFPOS"].ToString()
                                 & rows["ZUSA"].ToString() == ga["GASA"].ToString()
                                 & rows["ZUPOS"].ToString() == ga["GAPOS"].ToString()
                              select rows; 
                    foreach (DataRow rw in qzu)
                    { 
                        string sp = rw["SELLER"].ToString().Trim();
                        if(sppartie.Contains(sp+";"))
                            continue;
                        sppartie+=sp+";"; 
                    }
                }
                exAgent.SetCellValue(5, 13, GetGB2312Value(sppartie));
                #endregion 
            }
            #endregion  
        }
    }
}